﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ksp.Controller
{
    public class ShipParser
    {
        private static String Title = "ship = ";
        private static String Version = "version = ";
        private static String Description = "description = ";
        private static String Type = "type = ";

        public static Model.Ship ParseShip(List<String> lines)
        {
            Model.Ship ship = new Model.Ship();

            int lineNo = 0;
            while (lineNo < lines.Count-1)
            {
                #region Parsing
                if (lines[lineNo].StartsWith(Title))
                    ship.Title = lines[lineNo].Replace(Title, null);
                if (lines[lineNo].StartsWith(Version))
                    ship.Version = lines[lineNo].Replace(Version, null);
                if (lines[lineNo].StartsWith(Description))
                    ship.Description = ParseNewLine(lines[lineNo]);
                if (lines[lineNo].StartsWith(Type))
                {
                    switch (lines[lineNo].Replace(Type, null))
                    {
                        case "VAB":
                            ship.Type = Interface.Enums.ShipType.VAB;
                            break;
                        case "SPH":
                            ship.Type = Interface.Enums.ShipType.SPH;
                            break;
                    }
                }
                if (lines[lineNo].StartsWith(PartParser.KeyWord))
                {
                    List<String> partLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    Model.Part part = PartParser.ParsePart(partLines);
                    ship.Parts.Add(part);
                }
                #endregion
                lineNo++;
            }

            return ship;
        }

        private static char newLine = '¨';
        private static string ParseNewLine(String line)
        {
            return String.Join(Environment.NewLine, line.Replace(Description, null).Split(newLine));
        }


    }
}
